package ac.manchester.cs.afzal.autoservd.pattern_extraction;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.Vector;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Process_Predicate_Argument_Structures
{

    Vector list_dirs, local_sen_ids, occurrence_values, sen_ids,
            arguments, arguments1, distinct_sentences, doc_ids,
            dependencies, predicates;
    
    Vector[] dependency_statements;
    private String DEP_FILES_LOCATION = "DEP_FILES_LOCATION";
    private String DEP_STATEMENTS_LOCATION = "DEP_STATEMENTS_LOCATION";
    private String DISTINCT_PENN_LOCATION = "DISTINCT_PENN_LOCATION";
    private String PENN_FILES_LOCATION = "PENN_FILES_LOCATION";
    String[] Class =
    {
        "reports"
    };
    String[] Verbs =
    {
        "utilize", "validat", "view", "absorb", "achieve", "address", "adopt",
        "affect", "aims", "allow", "alter", "answer", "appear", "apply",
        "approach", "assess", "attend", "attests", "attribute", "based",
        "buffer", "capture", "change", "characterized", "collect", "compare",
        "composed", "conceptualize", "conduct", "confront", "connect",
        "consider", "constructed", "contribute", "couple", "deal", "debate",
        "define", "demonstrate", "describes", "detect", "develop", "disconnect",
        "discuss", "embed", "employ", "encompass", "engage", "enhanc", "evaluat",
        "examin", "exhibit", "expect", "explain", "exploit", "extend",
        "facilitat", "find", "focus", "formulate", "guide", "identify",
        "implement", "improv", "includ", "increase", "indicat", "influenc",
        "integrat", "intend", "investigat", "involve", "justify", "lead",
        "maintain", "mak", "map", "minimiz", "modeled", "need", "permit",
        "predict", "present", "proceed", "propos", "prov", "provide", "receiv",
        "reduc", "remain", "report", "requir", "resist", "reveal", "select",
        "show", "solv", "stress", "study", "suggest", "support", "survey",
        "synthesize", "tak", "tend", "test", "understand", "use"
    };

    File file_dir, file_doc, file_doc1;
    static private SQL_Handler_PE sqlHandler;
    private Properties props;

    public Process_Predicate_Argument_Structures()
    {
        sqlHandler = new SQL_Handler_PE();
        sqlHandler.connect_db();

        props = new Properties();
        try
        {
            props.load(new FileInputStream("AutoServD.properties"));
        }
        catch (Exception ex)
        {
            System.out.println("Exception while loading propoerties");
        }
        DEP_FILES_LOCATION = props.getProperty(DEP_FILES_LOCATION);
        DEP_STATEMENTS_LOCATION = props.getProperty(DEP_STATEMENTS_LOCATION);
        PENN_FILES_LOCATION = props.getProperty(PENN_FILES_LOCATION);
        DISTINCT_PENN_LOCATION = props.getProperty(DISTINCT_PENN_LOCATION);
    }

    public void load_both_arguments_from_predicates_using_dep_hybrid()
    {
        list_dirs = new Vector();
        local_sen_ids = new Vector();
        occurrence_values = new Vector();
        predicates = new Vector();
        dependencies = new Vector();

        doc_ids = new Vector();
        distinct_sentences = new Vector();

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String input;
        try
        {
            System.out.println(
                    "*****************************************************");
            System.out.print(
                    "Please Choose among following options:\n" +
                    " 1. Do you want to create new arguments tables (Both 'DEP' and 'HYBRID' ?  \n" +
                    " 2. Do you want to delete previous arguments entries \n" +
                    " 3. Otherwise \n" +
                    " Enter number to continue or 'e' to exit : ");
            input = in.readLine();
            input = input.trim();

            if (input.equals("1"))
            {
                sqlHandler.create_arguments_hybrid_table();
                sqlHandler.create_arguments_dep_table();
            }
            else if (input.equals("2"))
            {
                sqlHandler.delete_all(new String[]
                        {
                            "__predicates_arguments_hybrid",
                            "__predicates_arguments_dep"
                        });
                sqlHandler.reset_auto_inc(new String[]
                        {
                            "__predicates_arguments_hybrid",
                            "__predicates_arguments_dep"
                        });
            }
            else if (input.equals("e"))
            {
                System.exit(1);
            }
        }
        catch (Exception ex)
        {
            System.out.println(
                    "Value of Exception in reading input is : " + ex.getMessage());
        }

        list_dirs = sqlHandler.get_predicate_table_entries();
        local_sen_ids = sqlHandler.local_sen_ids;
        predicates = sqlHandler.predicates;
        dependencies = sqlHandler.dependencies;

        String document_name, predicate, local_sen_id, dependency;

        for (int i = 0; i < list_dirs.size(); i++)
        {
            document_name = list_dirs.elementAt(i).toString();
            predicate = predicates.elementAt(i).toString();
            local_sen_id = local_sen_ids.elementAt(i).toString();
            dependency = dependencies.elementAt(i).toString();

            occurrence_values = sqlHandler.get_details_per_predicate(document_name, local_sen_id);
            doc_ids = sqlHandler.document_ids;
            distinct_sentences = sqlHandler.distinct_sen_ids;


            String occurrence_value,doc_id, distinct_sentence;
            StringBuffer strBuf = new StringBuffer();
            StringBuffer strBuf1 = new StringBuffer();

            for (int j = 0; j < occurrence_values.size(); j++)
            {
                occurrence_value = occurrence_values.elementAt(j).toString();
                doc_id = doc_ids.elementAt(j).toString();
                distinct_sentence = distinct_sentences.elementAt(j).toString();

                strBuf = new StringBuffer();
                strBuf1 = new StringBuffer();

                arguments =  new Vector();
                arguments1 =  new Vector();

                if (Integer.parseInt(local_sen_id) < 10)
                {
                    file_doc = new File(PENN_FILES_LOCATION + document_name + "//00" + local_sen_id + "__" +
                            occurrence_value + ".xml");
                }
                else if (Integer.parseInt(local_sen_id) < 100)
                {
                    file_doc = new File(PENN_FILES_LOCATION + document_name + "//0" + local_sen_id + "__" +
                            occurrence_value + ".xml");
                }
                else
                {
                    file_doc = new File(PENN_FILES_LOCATION + document_name + "//" + local_sen_id + "__" +
                            occurrence_value + ".xml");
                }
                file_doc1 = new File(DISTINCT_PENN_LOCATION + doc_id + "//" + distinct_sentence + ".xml");

                try
                {
                    BufferedReader in1 = new BufferedReader(new FileReader(file_doc));
                    String str;
                    while ((str = in1.readLine()) != null)
                    {
                        strBuf.append(str);
                    }
                    in.close();
                }
                catch (IOException e)
                {
                    System.out.println("Exception in reading file : " + e.getMessage());
                }
                try
                {
                    BufferedReader in1 = new BufferedReader(new FileReader(file_doc1));
                    String str;
                    while ((str = in1.readLine()) != null)
                    {
                        strBuf1.append(str);
                    }
                    in.close();
                }
                catch (IOException e)
                {
                    System.out.println("Exception in reading file");
                }
                arguments.add(strBuf);
                arguments1.add(strBuf1);


                String statement = strBuf.toString();
                String statement1 = strBuf1.toString();

                String argument = " ";
                String argument1 = " ";
                String subject = "";
                String subject1 = "";

                int index_predicate = 0;
                int index_first_phrase = 0;
                int index_last_phrase = 0;
                int index_start_subj = 0;
                int index_end_subj = 0;

                if ((index_predicate = statement.indexOf(predicate, 0)) != -1)
                {

                    String temp = statement.substring(0, index_predicate);
                    index_first_phrase = temp.lastIndexOf("<st_" + "VP" + ">"); // NPs examples are 15319
                    index_last_phrase = statement.indexOf("</st_" + "VP" + ">", index_first_phrase);
                    try
                    {
                        if ((index_first_phrase == -1)||(index_last_phrase == -1))
                            argument = "ILLEGAL";
                        else
                            argument = statement.substring(index_first_phrase, index_last_phrase);
                        
                        if (((index_end_subj = temp.lastIndexOf("</st_NP>")) != -1)
                                &&(index_end_subj  != 0))
                        {
                            String temp_temp = temp.substring(0, index_end_subj);
                            index_start_subj = temp_temp.lastIndexOf("<st_NP>");
                            subject = temp_temp.substring(index_start_subj + 7, index_end_subj);
                            String patternStr = "<(.){0,10}>";
                            String replacementStr = " ";
                            Pattern pattern = Pattern.compile(patternStr);
                            Matcher matcher = pattern.matcher(subject);
                            subject = matcher.replaceAll(replacementStr);

                            for (int k = 0; k < 4; k++)
                            {
                                patternStr = "  ";
                                replacementStr = " ";
                                pattern = Pattern.compile(patternStr);
                                matcher = pattern.matcher(subject);
                                subject = matcher.replaceAll(replacementStr);
                            }
                            subject = subject.trim();
                        }
                    }
                    catch (Exception ex)
                    {
                        System.out.println("Exception is : " + ex.getMessage());
                    }

                    String patternStr = "<(.){0,10}>";
                    String replacementStr = " ";
                    Pattern pattern = Pattern.compile(patternStr);
                    Matcher matcher = pattern.matcher(argument);
                    argument = matcher.replaceAll(replacementStr);

                    for (int k = 0; k < 4; k++)
                    {
                        patternStr = "  ";
                        replacementStr = " ";
                        pattern = Pattern.compile(patternStr);
                        matcher = pattern.matcher(argument);
                        argument = matcher.replaceAll(replacementStr);
                    }
                    argument = argument.trim();
                }

                index_predicate = 0;
                index_first_phrase = 0;
                index_last_phrase = 0;

                if ((index_predicate = statement1.indexOf(predicate, 0)) != -1)
                {
                    String temp = statement1.substring(0, index_predicate);
                    index_first_phrase = temp.lastIndexOf("<st_" + "VP" + ">"); // NPs examples are 15319
                    index_last_phrase = statement1.indexOf("</st_" + "VP" + ">", index_first_phrase);
                    try
                    {
                        if ((index_first_phrase == -1)||(index_last_phrase == -1))
                            argument1 = "ILLEGAL";
                        else
                            argument1 = statement1.substring(index_first_phrase, index_last_phrase);

                        if ((index_end_subj = temp.lastIndexOf("</st_NP>")) != -1)
                        {
                            String temp_temp = temp.substring(0, index_end_subj);
                            index_start_subj = temp_temp.lastIndexOf("<st_NP>");
                            subject1 = temp_temp.substring(index_start_subj + 7, index_end_subj);
                            String patternStr = "<(.){0,10}>";
                            String replacementStr = " ";
                            Pattern pattern = Pattern.compile(patternStr);
                            Matcher matcher = pattern.matcher(subject1);
                            subject1 = matcher.replaceAll(replacementStr);

                            for (int k = 0; k < 4; k++)
                            {
                                patternStr = "  ";
                                replacementStr = " ";
                                pattern = Pattern.compile(patternStr);
                                matcher = pattern.matcher(subject1);
                                subject1 = matcher.replaceAll(replacementStr);
                            }
                            subject1 = subject1.trim();
                        }
                    }
                    catch (Exception ex)
                    {
                        System.out.println("Exception is : " + ex.getMessage());
                    }

                    String patternStr = "<(.){0,10}>";
                    String replacementStr = " ";
                    Pattern pattern = Pattern.compile(patternStr);
                    Matcher matcher = pattern.matcher(argument1);
                    argument1 = matcher.replaceAll(replacementStr);

                    for (int k = 0; k < 4; k++)
                    {
                        patternStr = "  ";
                        replacementStr = " ";
                        pattern = Pattern.compile(patternStr);
                        matcher = pattern.matcher(argument1);
                        argument1 = matcher.replaceAll(replacementStr);
                    }
                    subject = subject.replaceAll("'", "");
                    subject1 = subject1.replaceAll("'", "");
                    argument = argument.replaceAll("'", "");
                    argument1 = argument1.replaceAll("'", "");

                    String[] doc_table1 =
                    {
                        "predicate", "Arg1", "Arg2", "Arg1_X", "Arg2_X", "Term",
                        "Dep"
                    };
                    String[] doc_table_value1 =
                    {
                        predicate, subject, argument, subject1, argument1,
                        occurrence_value, dependency
                    };
                    sqlHandler.insert_table("__predicates_arguments_hybrid", doc_table1, doc_table_value1);

                    String[] doc_table2 =
                    {
                        "predicate", "Arg1", "Arg1_X", "Term", "Dep"
                    };
                    String[] doc_table_value2 =
                    {
                        predicate, argument, argument1, occurrence_value,
                        dependency
                    };
                    sqlHandler.insert_table("__predicates_arguments_dep", doc_table2, doc_table_value2);
                }
            }
        }
    }

    public void load_both_arguments_from_predicates_using_phrase()
    {
        this.reset_table("__predicates_arguments_ps");
        this.create_table("__predicates_arguments_ps");
        
        predicates = sqlHandler.get_predicates();

        for (int i = 0; i < predicates.size(); i++)
        {
            String predicate = predicates.elementAt(i).toString();
            String pattern = "%" + predicate + "%";

            list_dirs = new Vector();
            local_sen_ids = new Vector();
            occurrence_values = new Vector();
            distinct_sentences = new Vector();
            doc_ids = new Vector();


            list_dirs = sqlHandler.get_details_per_predicate_from_sentences(pattern);
            local_sen_ids = sqlHandler.local_sen_ids;
            occurrence_values = sqlHandler.occurrence_values;
            distinct_sentences = sqlHandler.distinct_sen_ids;
            doc_ids = sqlHandler.document_ids;


            arguments = new Vector();
            arguments1 = new Vector();

            for (int j = 0; j < list_dirs.size(); j++)
            {
                StringBuffer strBuf = new StringBuffer();
                StringBuffer strBuf1 = new StringBuffer();

                if (Integer.parseInt(local_sen_ids.elementAt(j).toString()) < 10)
                {
                    file_doc = new File(PENN_FILES_LOCATION + list_dirs.elementAt(j) + "//00" + local_sen_ids.elementAt(j) + "__" +
                            occurrence_values.elementAt(j) + ".xml");
                }
                else if (Integer.parseInt(local_sen_ids.elementAt(j).toString()) < 100)
                {
                    file_doc = new File(PENN_FILES_LOCATION + list_dirs.elementAt(j) + "//0" + local_sen_ids.elementAt(j) + "__" +
                            occurrence_values.elementAt(j) + ".xml");
                }
                else
                {
                    file_doc = new File(PENN_FILES_LOCATION + list_dirs.elementAt(j) + "//" + local_sen_ids.elementAt(j) + "__" +
                            occurrence_values.elementAt(j) + ".xml");
                }
                file_doc1 = new File(DISTINCT_PENN_LOCATION + doc_ids.elementAt(j) + "//" + distinct_sentences.elementAt(j) + ".xml");

                try
                {
                    BufferedReader in1 = new BufferedReader(new FileReader(file_doc));
                    String str;
                    while ((str = in1.readLine()) != null)
                    {
                        strBuf.append(str);
                    }
                    in1.close();
                }
                catch (IOException e)
                {
                    System.out.println("Exception in reading file DOC : " + e.getMessage());
                }

                try
                {
                    BufferedReader in2 = new BufferedReader(new FileReader(file_doc1));
                    String str;
                    while ((str = in2.readLine()) != null)
                    {
                        strBuf1.append(str);
                    }
                    in2.close();
                }
                catch (IOException e)
                {
                    System.out.println("Exception in reading file DOC 1: " + e.getMessage());
                }

                arguments.add(strBuf);
                arguments1.add(strBuf1);

                String statement = strBuf.toString();
                String statement1 = strBuf1.toString();

                int index_predicate = 0;
                int index_first_phrase = 0;
                int index_last_phrase = 0;
                int index_start_subj = 0;
                int index_end_subj = 0;
                String argument = " ";
                String subject = " ";

                if ((index_predicate = statement.indexOf(predicate, 0)) != -1)
                {
                    String temp = statement.substring(0, index_predicate);

                    index_first_phrase = temp.lastIndexOf("<st_" + "VP" + ">");
                    index_last_phrase = statement.indexOf("</st_" + "VP" + ">", index_first_phrase);
                    try
                    {
                        argument = statement.substring(index_first_phrase, index_last_phrase);

                        if ((index_end_subj = temp.lastIndexOf("</st_NP>")) != -1)
                        {
                            String temp_temp = temp.substring(0, index_end_subj);
                            index_start_subj = temp_temp.lastIndexOf("<st_NP>");
                            subject = temp_temp.substring(index_start_subj + 7, index_end_subj);

                            String patternStr = "<(.){0,10}>";
                            String replacementStr = " ";
                            Pattern pattern1 = Pattern.compile(patternStr);
                            Matcher matcher1 = pattern1.matcher(subject);
                            subject = matcher1.replaceAll(replacementStr);

                            for (int k = 0; k < 4; k++)
                            {
                                patternStr = "  ";
                                replacementStr = " ";
                                pattern1 = Pattern.compile(patternStr);
                                matcher1 = pattern1.matcher(subject);
                                subject = matcher1.replaceAll(replacementStr);
                            }
                            subject = subject.trim();
                        }
                    }
                    catch (Exception ex)
                    {
                        System.out.println("Exception is : " + ex.getMessage());
                    }

                    String patternStr = "<(.){0,10}>";
                    String replacementStr = " ";
                    Pattern pattern2 = Pattern.compile(patternStr);
                    Matcher matcher2 = pattern2.matcher(argument);
                    argument = matcher2.replaceAll(replacementStr);

                    for (int k = 0; k < 4; k++)
                    {
                        patternStr = "  ";
                        replacementStr = " ";
                        pattern2 = Pattern.compile(patternStr);
                        matcher2 = pattern2.matcher(argument);
                        argument = matcher2.replaceAll(replacementStr);
                    }
                    argument = argument.trim();
                }

                index_predicate = 0;
                index_first_phrase = 0;
                index_last_phrase = 0;
                index_start_subj = 0;
                index_end_subj = 0;
                String argument1 = " ";
                String subject1 = " ";

                if ((index_predicate = statement1.indexOf(predicate, 0)) != -1)
                {

                    String temp = statement1.substring(0, index_predicate);

                    index_first_phrase = temp.lastIndexOf("<st_" + "VP" + ">");
                    index_last_phrase = statement1.indexOf("</st_" + "VP" + ">", index_first_phrase);
                    try
                    {
                        argument1 = statement1.substring(index_first_phrase, index_last_phrase);

                        if ((index_end_subj = temp.lastIndexOf("</st_NP>")) != -1)
                        {
                            String temp_temp = temp.substring(0, index_end_subj);
                            index_start_subj = temp_temp.lastIndexOf("<st_NP>");
                            subject1 = temp_temp.substring(index_start_subj + 7, index_end_subj);
                            String patternStr = "<(.){0,10}>";
                            String replacementStr = " ";
                            Pattern pattern3 = Pattern.compile(patternStr);
                            Matcher matcher3 = pattern3.matcher(subject1);
                            subject1 = matcher3.replaceAll(replacementStr);

                            for (int k = 0; k < 4; k++)
                            {
                                patternStr = "  ";
                                replacementStr = " ";
                                pattern3 = Pattern.compile(patternStr);
                                matcher3 = pattern3.matcher(subject1);
                                subject1 = matcher3.replaceAll(replacementStr);
                            }
                            subject1 = subject1.trim();
                        }

                    }
                    catch (Exception ex)
                    {
                        System.out.println("Exception is : " + ex.getMessage());
                    }

                    String patternStr = "<(.){0,10}>";
                    String replacementStr = " ";
                    Pattern pattern3 = Pattern.compile(patternStr);
                    Matcher matcher3 = pattern3.matcher(argument1);
                    argument1 = matcher3.replaceAll(replacementStr);

                    for (int k = 0; k < 4; k++)
                    {
                        patternStr = "  ";
                        replacementStr = " ";
                        pattern3 = Pattern.compile(patternStr);
                        matcher3 = pattern3.matcher(argument1);
                        argument1 = matcher3.replaceAll(replacementStr);
                    }
                    argument1 = argument1.trim();

                    String[] doc_table1 =
                    {
                        "Predicate", "Arg1", "Arg2", "Arg1_X", "Arg2_X", "Term"
                    };
                    String[] doc_table_value1 =
                    {
                        predicate, subject, argument, subject1, argument1,
                        occurrence_values.elementAt(i).toString()
                    };
                    sqlHandler.insert_table("__predicates_arguments_ps", doc_table1, doc_table_value1);
                }
            }
        }
    }

    public void load_both_arguments_for_general_verbs_using_phrase()
    {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String input;
        try
        {
            System.out.println(
                    "*****************************************************");
            System.out.print(
                    "Please Choose among following options:\n" +
                    " 1. Do you want to create new arguments tables for Phrase-structure ?  \n" +
                    " 2. Do you want to delete previous arguments entries \n" +
                    " 3. Otherwise \n" +
                    " Enter number to continue or 'e' to exit : ");
            input = in.readLine();
            input = input.trim();

            if (input.equals("1"))
            {
                sqlHandler.create_arguments_phrase_gcs_table();
            }
            else if (input.equals("2"))
            {
                sqlHandler.delete_all(new String[]
                        {
                            "__predicates_arguments_ps_gcs"
                        });
                sqlHandler.reset_auto_inc(new String[]
                        {
                            "__predicates_arguments_ps_gcs"
                        });
            }
            else if (input.equals("e"))
            {
                System.exit(1);
            }
        }
        catch (Exception ex)
        {
            System.out.println("SQL Exception: Reading input: " + ex.getMessage());
        }


        for (int i = 0; i < Verbs.length; i++)
        {
            String predicate = Verbs[i];
            String pattern = "%" + predicate + "%";

            list_dirs = new Vector();
            local_sen_ids = new Vector();
            occurrence_values = new Vector();
            distinct_sentences = new Vector();
            doc_ids = new Vector();


            list_dirs = sqlHandler.get_details_per_predicate_from_sentences(pattern);
            local_sen_ids = sqlHandler.local_sen_ids;
            occurrence_values = sqlHandler.occurrence_values;
            distinct_sentences = sqlHandler.distinct_sen_ids;
            doc_ids = sqlHandler.document_ids;


            arguments = new Vector();
            arguments1 = new Vector();

            for (int j = 0; j < list_dirs.size(); j++)
            {
                StringBuffer strBuf = new StringBuffer();
                StringBuffer strBuf1 = new StringBuffer();

                if (Integer.parseInt(local_sen_ids.elementAt(j).toString()) < 10)
                {
                    file_doc = new File(PENN_FILES_LOCATION + list_dirs.elementAt(j) + "//00" + local_sen_ids.elementAt(j) + "__" +
                            occurrence_values.elementAt(j) + ".xml");
                }
                else if (Integer.parseInt(local_sen_ids.elementAt(j).toString()) < 100)
                {
                    file_doc = new File(PENN_FILES_LOCATION + list_dirs.elementAt(j) + "//0" + local_sen_ids.elementAt(j) + "__" +
                            occurrence_values.elementAt(j) + ".xml");
                }
                else
                {
                    file_doc = new File(PENN_FILES_LOCATION + list_dirs.elementAt(j) + "//" + local_sen_ids.elementAt(j) + "__" +
                            occurrence_values.elementAt(j) + ".xml");
                }
                file_doc1 = new File(DISTINCT_PENN_LOCATION + doc_ids.elementAt(j) + "//" + distinct_sentences.elementAt(j) + ".xml");

                try
                {
                    BufferedReader in1 = new BufferedReader(new FileReader(file_doc));
                    String str;
                    while ((str = in1.readLine()) != null)
                    {
                        strBuf.append(str);
                    }
                    in.close();
                }
                catch (IOException e)
                {
                    System.out.println("Exception in reading file");
                }

                try
                {
                    BufferedReader in2 = new BufferedReader(new FileReader(file_doc1));
                    String str;
                    while ((str = in2.readLine()) != null)
                    {
                        strBuf1.append(str);
                    }
                    in.close();
                }
                catch (IOException e)
                {
                    System.out.println("Exception in reading file");
                }

                arguments.add(strBuf);
                arguments1.add(strBuf1);

                String statement = strBuf.toString();
                String statement1 = strBuf1.toString();

                int index_predicate = 0;
                int index_first_phrase = 0;
                int index_last_phrase = 0;
                int index_start_subj = 0;
                int index_end_subj = 0;
                String argument = " ";
                String subject = " ";

                if ((index_predicate = statement.indexOf(predicate, 0)) != -1)
                {
                    String temp = statement.substring(0, index_predicate);

                    index_first_phrase = temp.lastIndexOf("<st_" + "VP" + ">");
                    index_last_phrase = statement.indexOf("</st_" + "VP" + ">", index_first_phrase);
                    try
                    {
                        argument = statement.substring(index_first_phrase, index_last_phrase);

                        if ((index_end_subj = temp.lastIndexOf("</st_NP>")) != -1)
                        {
                            String temp_temp = temp.substring(0, index_end_subj);
                            index_start_subj = temp_temp.lastIndexOf("<st_NP>");
                            subject = temp_temp.substring(index_start_subj + 7, index_end_subj);

                            String patternStr = "<(.){0,10}>";
                            String replacementStr = " ";
                            Pattern pattern1 = Pattern.compile(patternStr);
                            Matcher matcher1 = pattern1.matcher(subject);
                            subject = matcher1.replaceAll(replacementStr);

                            for (int k = 0; k < 4; k++)
                            {
                                patternStr = "  ";
                                replacementStr = " ";
                                pattern1 = Pattern.compile(patternStr);
                                matcher1 = pattern1.matcher(subject);
                                subject = matcher1.replaceAll(replacementStr);
                            }
                            subject = subject.trim();
                        }

                    }
                    catch (Exception ex)
                    {
                        System.out.println("Exception is : " + ex.getMessage());
                    }

                    String patternStr = "<(.){0,10}>";
                    String replacementStr = " ";
                    Pattern pattern2 = Pattern.compile(patternStr);
                    Matcher matcher2 = pattern2.matcher(argument);
                    argument = matcher2.replaceAll(replacementStr);

                    for (int k = 0; k < 4; k++)
                    {
                        patternStr = "  ";
                        replacementStr = " ";
                        pattern2 = Pattern.compile(patternStr);
                        matcher2 = pattern2.matcher(argument);
                        argument = matcher2.replaceAll(replacementStr);
                    }
                    argument = argument.trim();
                }

                index_predicate = 0;
                index_first_phrase = 0;
                index_last_phrase = 0;
                index_start_subj = 0;
                index_end_subj = 0;
                String argument1 = " ";
                String subject1 = " ";

                if ((index_predicate = statement1.indexOf(predicate, 0)) != -1)
                {

                    String temp = statement1.substring(0, index_predicate);

                    index_first_phrase = temp.lastIndexOf("<st_" + "VP" + ">");
                    index_last_phrase = statement1.indexOf("</st_" + "VP" + ">", index_first_phrase);
                    try
                    {
                        argument1 = statement1.substring(index_first_phrase, index_last_phrase);

                        if ((index_end_subj = temp.lastIndexOf("</st_NP>")) != -1)
                        {
                            String temp_temp = temp.substring(0, index_end_subj);
                            index_start_subj = temp_temp.lastIndexOf("<st_NP>");
                            subject1 = temp_temp.substring(index_start_subj + 7, index_end_subj);
                            String patternStr = "<(.){0,10}>";
                            String replacementStr = " ";
                            Pattern pattern3 = Pattern.compile(patternStr);
                            Matcher matcher3 = pattern3.matcher(subject1);
                            subject1 = matcher3.replaceAll(replacementStr);

                            for (int k = 0; k < 4; k++)
                            {
                                patternStr = "  ";
                                replacementStr = " ";
                                pattern3 = Pattern.compile(patternStr);
                                matcher3 = pattern3.matcher(subject1);
                                subject1 = matcher3.replaceAll(replacementStr);
                            }
                            subject1 = subject1.trim();
                        }

                    }
                    catch (Exception ex)
                    {
                        System.out.println("Exception is : " + ex.getMessage());
                    }

                    String patternStr = "<(.){0,10}>";
                    String replacementStr = " ";
                    Pattern pattern3 = Pattern.compile(patternStr);
                    Matcher matcher3 = pattern3.matcher(argument1);
                    argument1 = matcher3.replaceAll(replacementStr);

                    for (int k = 0; k < 4; k++)
                    {
                        patternStr = "  ";
                        replacementStr = " ";
                        pattern3 = Pattern.compile(patternStr);
                        matcher3 = pattern3.matcher(argument1);
                        argument1 = matcher3.replaceAll(replacementStr);
                    }
                    argument1 = argument1.trim();

                    String[] doc_table1 =
                    {
                        "Predicate", "Arg1", "Arg2", "Arg1_X", "Arg2_X", "Term"
                    };
                    String[] doc_table_value1 =
                    {
                        predicate, subject, argument, subject1, argument1,
                        occurrence_values.elementAt(i).toString()
                    };
                    sqlHandler.insert_table("__predicates_arguments_ps_gcs", doc_table1, doc_table_value1);
                }
            }
        }
    }
        /**
     * Reset the given table
     * @param table_name
     */
    public void reset_table(String table_name)
    {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String input;
        try
        {
            System.out.print("Do you want to delete previous entries in table " + table_name + " ? \n" +
                    " Enter 'y' for yes, 'n' for no, or 'e' to exit : ");
            input = in.readLine();
            input = input.trim();

            if(input.equals("y"))
            {
                sqlHandler.delete_all(new String[]
                        {
                            table_name
                        });
                sqlHandler.reset_auto_inc(new String[]
                        {
                            table_name
                        });
            }
            else if(input.equals("e"))
            {
                System.exit(1);
            }
        }
        catch (Exception ex)
        {
            System.out.println("Exception: Reading User's input : " + ex.getMessage());
        }
    }
        /**
     * Reset the given table
     * @param table_name
     */
    public void create_table(String table_name)
    {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String input;
        try
        {
            System.out.println(
                    "*****************************************************");
            System.out.print(
                    " 1. Do you want to create new tables " + table_name + " ?  \n" +
                    " Enter 'n' for no and 'e' to exit : ");
            input = in.readLine();
            input = input.trim();

            if (input.equals("1"))
            {
                if (table_name.equals("__predicates_arguments_ps"))
                    sqlHandler.create_arguments_phrase_table();
                else if(table_name.equals(input))
                    ;

            }
            else if (input.equals("n"))
            {
                System.out.println("You have opted to work with existing table");
            }
            else if (input.equals("e"))
            {
                System.exit(1);
            }
        }
        catch (Exception ex)
        {
            System.out.println(
                    "SQL Exception: Taking input : " + ex.getMessage());
        }
    }

}

 
 
